package it.disim.tlp.webreputation.business.impl;

import it.disim.tlp.webreputation.business.AbstractDataAggregatorFactory;
import it.disim.tlp.webreputation.business.ConnectorService;
import it.disim.tlp.webreputation.business.FactoryBuilder;
import it.disim.tlp.webreputation.business.SorgenteService;
import it.disim.tlp.webreputation.business.UtenteService;
import it.disim.tlp.webreputation.business.model.Connector;
import it.disim.tlp.webreputation.business.model.Sorgente;
import it.disim.tlp.webreputation.business.model.Utente;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class JDBCConnectorService implements ConnectorService{

	
	/**
	 * The method takes all the connectors in the DB (objects of type Connector ()) and returns them in a collection.
	 * @return Collectio<Connector> Returns a collection of objects of type Connector ().
	 */
	@Override
	public Collection<Connector> getAllConnectors() {
		
		Connection con = DBConnect.getConnection();			
						
		Statement st = null;
		ResultSet rs = null;
		List<Connector> result = new ArrayList<Connector>();
		try {
			st = con.createStatement();			

			rs = st.executeQuery("SELECT * FROM connettori");
			while (rs.next()) {		
				
				Connector conn = this.getConnector(new Connector(rs.getInt("ID")));				
				result.add(conn);
				
			}
			

		} catch (SQLException e) {
			e.printStackTrace();			
		} finally {
			if (st!=null) {
				try {st.close();} catch (SQLException e) {}
			}

		}
		return result;
	}

	
	/**
	 * The method incorporates a connector via its id. However, takes as input an object of type connector ()
	 * which typically has only the ID field filled.
	 * @param connector Takes as input an object of type Connector () filled with only the ID.
	 * @return connector Returns an object of type Connector () completely filled with data from the DB.
	 */
	@Override
	public Connector getConnector(Connector connector) {
		
		Connection con = DBConnect.getConnection();
		
		AbstractDataAggregatorFactory factory = FactoryBuilder.getInstance();
						
		Statement st = null;
		ResultSet rs = null;
		Connector result = null;
		try {
			st = con.createStatement();			

			rs = st.executeQuery("SELECT * FROM connettori WHERE ID = " + connector.getId());
			if (rs.next()) {		
				
				Collection<Sorgente> sorgenti = new ArrayList<Sorgente>();
				
				//The list of sources we require the SorgenteService
				SorgenteService sorgenteService = factory.getSorgenteService();
				sorgenti = sorgenteService.getSorgentiFromConnector(connector);
				
				int id = rs.getInt("ID");
				String name = rs.getString("Nome");	
				int updateInterval = rs.getInt("UpdateInterval");
				
				result = new Connector(id, name, updateInterval, sorgenti);
				
				
			}
			

		} catch (SQLException e) {
			e.printStackTrace();			
		} finally {
			if (st!=null) {
				try {st.close();} catch (SQLException e) {}
			}

		}
		return result;
	}

}
